﻿using TouchTable.Event;
using UnityEngine;

namespace UnityTouchTable.Runtime
{
    /// <summary>
    /// 识别桌倾听器
    /// </summary>
    [AddComponentMenu("TouchTable/TouchTableListener", 2)]
    [DisallowMultipleComponent]
    public class TouchTableListener : MonoBehaviour
    {
        #region Field
        /// <summary>
        /// 识别桌入口
        /// </summary>
        [SerializeField]
        [Header("识别桌入口")]
        private UnityTouchTableEntry m_Entry = default;
        #endregion

        private void OnEnable()
        {
            if (this.m_Entry == null)
                return;

            this.m_Entry.OnAddMarks += this.OnAddMarks;
            this.m_Entry.OnUpdateMarks += this.OnUpdateMarks;
            this.m_Entry.OnRemoveMarks += this.OnRemoveMarks;
        }

        private void OnDisable()
        {
            if (this.m_Entry != null)
            {
                this.m_Entry.OnAddMarks -= this.OnAddMarks;
                this.m_Entry.OnUpdateMarks -= this.OnUpdateMarks;
                this.m_Entry.OnRemoveMarks -= this.OnRemoveMarks;
            }
        }

        private void OnAddMarks(object sender, MarkEventArgs e)
        {
            Debug.Log($"add: {e.Markers[0]}");
        }

        private void OnUpdateMarks(object sender, MarkEventArgs e)
        {
            Debug.Log($"update: {e.Markers[0].CurrentAngle}");
        }

        private void OnRemoveMarks(object sender, MarkEventArgs e)
        {
            Debug.Log($"remove: {e.Markers[0]}");
        }
    }
}